using System;
using System.Collections.Generic;
using UnityEngine;

public class FlagGridUI : MonoBehaviour
{
	[SerializeField]
	private Color _color;

	[SerializeField]
	private GameObject _go;

	private UIGrid _grid;

	[SerializeField]
	private UISprite _sprite;

	[SerializeField]
	private SpriteWithBackground _pixelmonTexture;

	private List<GameObject> listObject = new List<GameObject>();

	private void Awake()
	{
		_grid = GetComponent<UIGrid>();
	}

	public void CountObject(int count, string[] _name)
	{
		if (count == listObject.Count || count < 0)
		{
			return;
		}
		if (count > listObject.Count)
		{
			for (int i = listObject.Count; i < count; i++)
			{
				GameObject gameObject = UnityEngine.Object.Instantiate(_go);
				gameObject.transform.SetParent(_grid.transform);
				gameObject.transform.position = Vector3.zero;
				gameObject.transform.localScale = Vector3.one;
				gameObject.gameObject.SetActive(true);
				listObject.Add(gameObject);
			}
			_grid.repositionNow = true;
		}
		else if (count < listObject.Count)
		{
			while (listObject.Count != count)
			{
				UnityEngine.Object.Destroy(listObject[listObject.Count - 1].gameObject);
				listObject.RemoveAt(listObject.Count - 1);
			}
			_grid.repositionNow = true;
		}
		int num = 0;
		foreach (GameObject item in listObject)
		{
			UISprite[] componentsInChildren = item.GetComponentsInChildren<UISprite>(true);
			foreach (UISprite uISprite in componentsInChildren)
			{
				if (uISprite.name == _name[num])
				{
					uISprite.gameObject.SetActive(true);
				}
				else
				{
					uISprite.gameObject.SetActive(false);
				}
				uISprite.GetComponent<UISprite>().color = _color;
			}
			item.gameObject.name = _name[num];
			num++;
		}
	}

	public void CreateSprite(string[] arrayName)
	{
		if (_sprite == null)
		{
			return;
		}
		foreach (GameObject item in listObject)
		{
			UnityEngine.Object.Destroy(item);
		}
		listObject.Clear();
		if (arrayName == null)
		{
			return;
		}
		foreach (string spriteName in arrayName)
		{
			UISprite uISprite = UnityEngine.Object.Instantiate(_sprite);
			uISprite.transform.SetParent(_grid.transform);
			uISprite.transform.position = Vector3.zero;
			uISprite.transform.localScale = Vector3.one;
			uISprite.spriteName = spriteName;
			uISprite.gameObject.name = spriteName;
			listObject.Add(uISprite.gameObject);
		}
		_grid.repositionNow = true;
		foreach (GameObject item2 in listObject)
		{
			item2.SetActive(false);
		}
	}

	public void CreateSpritePixelmoon(string[] arrayName)
	{
		foreach (GameObject item in listObject)
		{
			UnityEngine.Object.Destroy(item);
		}
		listObject.Clear();
		if (arrayName == null)
		{
			return;
		}
		foreach (string text in arrayName)
		{
			SpriteWithBackground spriteWithBackground = UnityEngine.Object.Instantiate(_pixelmonTexture);
			string[] array = text.Split(new char[1] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
			BCWFlagControllerUI componentInParent = GetComponentInParent<BCWFlagControllerUI>();
			if (array.Length == 2)
			{
				float alpha = spriteWithBackground._background.alpha;
				switch (array[1])
				{
				case "our":
					if (componentInParent != null)
					{
						spriteWithBackground._background.color = componentInParent._ourColor;
					}
					else
					{
						spriteWithBackground._background.color = Color.blue;
					}
					break;
				case "enemy":
					if (componentInParent != null)
					{
						spriteWithBackground._background.color = componentInParent._enemyColor;
					}
					else
					{
						spriteWithBackground._background.color = Color.red;
					}
					break;
				}
				spriteWithBackground._background.alpha = alpha;
			}
			spriteWithBackground.transform.SetParent(_grid.transform);
			spriteWithBackground.transform.position = Vector3.zero;
			spriteWithBackground.transform.localScale = Vector3.one;
			spriteWithBackground._mainTexture.spriteName = array[0];
			spriteWithBackground.gameObject.name = text;
			listObject.Add(spriteWithBackground.gameObject);
		}
		_grid.repositionNow = true;
		foreach (GameObject item2 in listObject)
		{
			item2.SetActive(true);
		}
	}
}
